package com.ccsoft.atools.service;


import com.ccsoft.atools.dao.CashTypeMapper;
import com.ccsoft.atools.model.Cash;
import com.ccsoft.atools.model.CashType;
import com.ccsoft.atools.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CashTypeServiceImpl implements CashTypeService {

    @Autowired
    private CashTypeMapper dao;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        CashType record = selectByPrimaryKey(iid);
        if (record == null)
            throw new BusinessException("不存在该数据，无法删除！");

        if (hasData(record.getSid()) == true)
            throw new BusinessException("有相关业务数据，不可删除！");
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public CashType insert(CashType record) {
        if (isExist(record.getColpay(), record.getSid()) == true)
            throw new BusinessException("编号：" + record.getSid() + ",已经存在！");
        int ret = dao.insert(record);
        return record;
    }

    @Override
    public CashType insertSelective(CashType record) {
        if (isExist(record.getColpay(), record.getSid()) == true)
            throw new BusinessException("编号：" + record.getSid() + ",已经存在！");
        int ret = dao.insertSelective(record);
        return record;
    }

    @Override
    public CashType selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public CashType updateByPrimaryKeySelective(CashType record) {
        int ret = dao.updateByPrimaryKeySelective(record);
        return record;
    }

    @Override
    public CashType updateByPrimaryKey(CashType record) {
        int ret = dao.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public List<CashType> selectAll() {
        return dao.selectAll();
    }

    @Override
    public CashType selectBySid(String colpay, String sid) {
        return dao.selectBySid(colpay, sid);
    }

    @Override
    public boolean isExist(String colpay, String sid) {
        if (sid == null || sid.length() <= 0) return false;
        CashType record = dao.selectBySid(colpay, sid);
        return record == null ? false : true;
    }

    @Override
    public List<CashType> selectByType(String type) {
        return dao.selectByType(type);
    }

    @Override
    public boolean hasData(String sid) {
        List<Cash> list = dao.hasData(sid);
        return list.size() > 0 ? true : false;
    }


}
